package com.ezgo.erc.mq.service;

import java.io.Serializable;
import java.util.Map;
import java.util.stream.Stream;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class ProducerServiceImpl implements BaseMqService {
	@Autowired
	private JmsTemplate jmsTemplate;

	@Override
	public void sendMessage(Destination destination, final String message) {
		System.out.println("---------------String生产者发送消息-----------------");
		System.out.println("---------------String生产者发了一个消息：" + message);
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(message);
			}
		});
	}

	@Override
	public void sendMessage(Destination destination, final Byte[] bytes) {
		System.out.println("---------------Byte生产者发送消息-----------------");
		System.out.println("---------------Byte生产者发了一个消息：" + bytes.toString());
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createBytesMessage();
			}
		});
	}

	@Override
	public void sendMessage(Destination destination, final Map<?, ?> map) {
		System.out.println("---------------Map生产者发送消息-----------------");
		System.out.println("---------------Map生产者发了一个消息：" + map.toString());
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createMapMessage();
			}
		});
	}

	@Override
	public void sendMessage(Destination destination, final Serializable obj) {
		System.out.println("---------------Object生产者发送消息-----------------");
		System.out.println("---------------Object生产者发了一个消息：" + obj.toString());
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage(obj);
			}
		});
		jmsTemplate.convertAndSend(destination, obj);
	}

	@Override
	public void sendMessage(Destination destination, final Stream<?> stream) {
		System.out.println("---------------Stream生产者发送消息-----------------");
		System.out.println("---------------Stream生产者发了一个消息：" + stream.sequential());
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createStreamMessage();
			}
		});
	}

}
